package com.cskaoyan.wordmemorize.controller.app;


import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.PayOrderDTO;
import com.cskaoyan.wordmemorize.dto.app.VocPaySaleDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.PaySuccessNotifyCommand;
import com.cskaoyan.wordmemorize.request.VocPaySaleCommand;
import com.cskaoyan.wordmemorize.service.UserPayService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(name = "移动端-用户(支付)模块")
@RestController
@RequestMapping("user")
public class PayController {

    @Resource
    UserPayService userPayService;
    //用户用于支付
    @PostMapping("pay")
    public Result<PayOrderDTO> pay(@RequestBody VocPaySaleCommand command) {
        //获取用户id
        long userId = StpKit.USER.getLoginIdAsLong();
        PayOrderDTO pay = userPayService.pay(command, userId);
        return Result.ok(pay);
    }
    //用户用于获取支付结果
    @GetMapping("pay/{paySaleId}")
    public Result<Integer> getPayResult(@PathVariable String paySaleId) {
        int i = userPayService.queryTradeStatus(Long.parseLong(paySaleId));
        return Result.ok(i);
    }
    //用户用于查看购买历史
    @GetMapping("pay/list")
    public Result<PageDTO<VocPaySaleDTO>> getPayHistory(PageRequest pageRequest) {
        PageDTO<VocPaySaleDTO> paySaleHistory = userPayService.getPaySaleHistory(StpKit.USER.getLoginIdAsLong(), pageRequest);
        return Result.ok(paySaleHistory);
    }
    //用户用于接收支付成功的异步通知
    @PostMapping("pay/notice")
    public Result<String> payNotice(@RequestBody PaySuccessNotifyCommand command) {
        String result = userPayService.paySuccess(command, command.getUserId());
        return Result.ok(result);
    }

}
